<template>
  <div class="detail-container">
    <h3>健康检测详情</h3>
    <el-card class="detail-card">
      <el-skeleton :loading="loading" animated>
        <template #template>
          <el-skeleton-item variant="h3" style="width: 200px; margin-bottom: 16px;" />
          <el-skeleton-item variant="text" style="margin-bottom: 12px;" v-for="n in 10" :key="n" />
        </template>
        <template #default>
          <div v-if="error" class="error-message">
            <p>系统异常：{{ error }}</p>
          </div>
          <div v-else>
            <div class="detail-item">
              <label>检测类型：</label><span>{{ detail.recordType }}</span>
            </div>
            <div class="detail-item">
              <label>身高 (cm)：</label><span>{{ detail.height }}</span>
            </div>
            <div class="detail-item">
              <label>体重 (kg)：</label><span>{{ detail.weight }}</span>
            </div>
            <div class="detail-item" v-if="detail.bloodPressure">
              <label>血压 (mmHg)：</label><span>{{ detail.bloodPressure }}</span>
            </div>
            <div class="detail-item" v-else>
              <label>收缩压 / 舒张压：</label>
              <span>{{ detail.systolicPressure }} / {{ detail.diastolicPressure }}</span>
            </div>
            <div class="detail-item">
              <label>血糖 (mmol/L)：</label><span>{{ detail.bloodSugar }}</span>
            </div>
            <div class="detail-item">
              <label>心率 (次/分钟)：</label><span>{{ detail.heartRate }}</span>
            </div>
            <div class="detail-item">
              <label>体温 (℃)：</label><span>{{ detail.temperature }}</span>
            </div>
            <div class="detail-item">
              <label>健康内容摘要：</label><span>{{ detail.content }}</span>
            </div>
            <div class="detail-item">
              <label>备注：</label><span>{{ detail.notes }}</span>
            </div>
            <div class="detail-item">
              <label>记录时间：</label><span>{{ formatDate(detail.createTime) }}</span>
            </div>
          </div>
        </template>
      </el-skeleton>

      <div class="detail-footer" v-if="!loading">
        <el-button type="primary" @click="goBack">返回</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getHealthDataDetailApi } from '../api/healthcheck'

const route = useRoute()
const router = useRouter()

const detail = ref({
  recordType: '',
  height: null,
  weight: null,
  bloodPressure: '',
  systolicPressure: null,
  diastolicPressure: null,
  bloodSugar: null,
  heartRate: null,
  temperature: null,
  content: '',
  notes: '',
  createTime: ''
})
const loading = ref(false)
const error = ref('')

const formatDate = date => date ? new Date(date).toLocaleString() : ''

const goBack = () => router.back()

onMounted(async () => {
  const id = route.params.id
  console.log('[HealthDetail] route.params.id =', id)
  if (!id) {
    error.value = '无效的记录ID'
    console.error('[HealthDetail] 无效的记录ID')
    return
  }

  loading.value = true
  try {
    const res = await getHealthDataDetailApi(id)
    console.log('[HealthDetail] API 返回 res =', res)
    // 修改：后端成功码改为 200
    if (res.code !== 200) {
      error.value = `后端错误：${res.message || '未知错误'} (code=${res.code})`
      console.error('[HealthDetail] 后端错误：', res)
    } else if (!res.data) {
      error.value = '后端未返回 data'
      console.error('[HealthDetail] res.data 为空')
    } else {
      detail.value = res.data
      console.log('[HealthDetail] detail.value =', detail.value)
    }
  } catch (err) {
    console.error('[HealthDetail] 请求异常：', err)
    if (err.response) {
      console.error('[HealthDetail] err.response.data =', err.response.data)
      console.error('[HealthDetail] err.response.status =', err.response.status)
      error.value = `HTTP ${err.response.status}：${JSON.stringify(err.response.data)}`
    } else if (err.request) {
      console.error('[HealthDetail] err.request =', err.request)
      error.value = '无响应，请检查网络或后端服务'
    } else {
      error.value = err.message
    }
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.detail-container {
  padding: 16px;
  max-width: 800px;
  margin: 0 auto;
}
.detail-card {
  margin-top: 20px;
}
.detail-item {
  margin-bottom: 15px;
  display: flex;
  align-items: baseline;
}
.detail-item label {
  font-weight: bold;
  margin-right: 10px;
  color: #606266;
  min-width: 120px;
}
.detail-item span {
  color: #303133;
  flex: 1;
}
.detail-footer {
  margin-top: 20px;
  text-align: right;
}
.error-message {
  color: #f56c6c;
  padding: 20px;
  background: #fff0f0;
  border: 1px solid #fde2e2;
  border-radius: 4px;
}
</style>
